package com.cwj.io.nio;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.FileChannel;

/**
 * @author: Chenwujie
 * @date: 2021-04-13 11:29
 */
public class BufferTest {
    public static void main(String[] args) throws IOException {
        RandomAccessFile randomAccessFile =
            new RandomAccessFile("src\\main\\resources\\test.txt", "rw");
        FileChannel channel = randomAccessFile.getChannel();
        ByteBuffer buffer = ByteBuffer.allocate(1024);
        CharBuffer charBuffer = CharBuffer.allocate(1024);
        int read = channel.read(buffer);
        while (read != -1){
            System.out.println("read: " + read);
            charBuffer = buffer.asCharBuffer();
//            charBuffer.flip();
            while(charBuffer.hasRemaining()){
                System.out.print(charBuffer.get());
            }
            charBuffer.clear();
            buffer.clear();
            read = channel.read(buffer);
        }
        randomAccessFile.close();
    }
}
